package StudyCode.summary.myfile;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DeleteDirectoryWithSimpleFileVisitor {
	public static void main(String[] args) {
		try {
			Files.walkFileTree(Paths.get("E:/Move"), new DeleteDirectoryImplementsSimpleFileVisitor());
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

}

class DeleteDirectoryImplementsSimpleFileVisitor extends SimpleFileVisitor<Path> {
	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
			throws IOException {
		System.out.println("Deleting " + file.getFileName());
		Files.delete(file);
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult postVisitDirectory(Path directory,
	                                          IOException exception) throws IOException {
		if (exception == null) {
			System.out.println("Deleting " + directory.getFileName());
			Files.delete(directory);
			return FileVisitResult.CONTINUE;
		} else {
			throw exception;
		}
	}
}
